package com.wthink.hzgj.gpsInfo.viewModel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.data.Result
import com.wthink.hzgj.data.domain.GpsInfo
import com.wthink.hzgj.service.DevService
import com.wthink.hzgj.util.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class GpsInfoViewModel :ViewModel(){

    val gpsInfo = MutableLiveData(GpsInfo("","","","",""
        ,"","","","","","","","","",""))

   fun getGpsInfos(){
       viewModelScope.launch(Dispatchers.IO) {
           val gpsInfos = DevService.queryGpsInfo()
           if (gpsInfos is Result.Success) {
               val data = gpsInfos.data
               gpsInfo.postValue(data)
           }else if (gpsInfos is Result.Error) {
               withContext(Dispatchers.Main) {
                   toast(BusApplication.busContext, gpsInfos.exception.toString())
               }
           }
       }
   }
}